{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Array Functions\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "from numpy import (\r\n",
    "    arange,\r\n",
    "    array,\r\n",
    "    diag,\r\n",
    "    dot,\r\n",
    "    eye,\r\n",
    "    hstack,\r\n",
    "    kron,\r\n",
    "    prod,\r\n",
    "    reshape,\r\n",
    "    tile,\r\n",
    "    trace,\r\n",
    "    vstack,\r\n",
    ")\r\n",
    "from numpy.linalg import cholesky, det, eig, eigvals, inv, lstsq, matrix_rank\r\n",
    "from numpy.random import randn"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 1\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x = arange(12.0)\r\n",
    "x.shape = 2, 6\r\n",
    "x.shape = 3, 4\r\n",
    "x.shape = 4, 3\r\n",
    "x.shape = 6, 2\r\n",
    "x.shape = 2, 2, 3\r\n",
    "x.shape = 12"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 2\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x = reshape(arange(12.0), (4, 3))\r\n",
    "x"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"x.flatten()[1::2]:\")\n",
    "x.flatten()[1::2]"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"x.ravel()[1::2]:\")\n",
    "x.ravel()[1::2]"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"x.flat[1::2]:\")\n",
    "x.flat[1::2]"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 3\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x = randn(2, 2)\n",
    "y = randn(1, 1)\n",
    "z = randn(3, 2)\n",
    "pt1 = hstack((x, tile(y, (2, 3))))\n",
    "pt2 = vstack((z.T, tile(y, (1, 3))))\n",
    "pt3 = hstack((z, pt2))\n",
    "final = vstack((pt1, pt3))\n",
    "final"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 4\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x = reshape(arange(12.0), (2, 2, 3))\n",
    "print(\"x.squeeze():\")\n",
    "x.squeeze()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 5\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "y = array([[2, 0.5], [0.5, 4]])\n",
    "z = diag(diag(y))\n",
    "z"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 6\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "z = cholesky(y)\n",
    "z"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"dot(z,z.T):\")\n",
    "dot(z, z.T)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 7\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"trace(y):\")\n",
    "trace(y)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "D = eigvals(y)\n",
    "print(\"sum(D):\")\n",
    "sum(D)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 7b\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"det(y):\")\n",
    "det(y)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"prod(D):\")\n",
    "prod(D)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 8\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"inv(y):\")\n",
    "inv(y)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "(D, V) = eig(y)\n",
    "D = 1 / D\n",
    "print(\"dot(dot(V,diag(D)),V.T):\")\n",
    "dot(dot(V, diag(D)), V.T)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 9\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x = randn(100, 2)\n",
    "e = randn(100, 1)\n",
    "B = array([[1], [0.5]])\n",
    "y = dot(x, B) + e\n",
    "out = lstsq(x, y, rcond=None)\n",
    "estimate = out[0]\n",
    "estimate"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 10\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "y = array([[5, -1.5, -3.5], [-1.5, 2, -0.5], [-3.5, -0.5, 4]])\n",
    "D = eigvals(y)\n",
    "print(\"matrix_rank(y):\")\n",
    "matrix_rank(y)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"det(y):\")\n",
    "det(y)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 11\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x = randn(100, 2)\n",
    "SigmaX = dot(x.T, x) / 100\n",
    "print(\"kron(eye(2),SigmaX):\")\n",
    "kron(eye(2), SigmaX)"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}